package com.edwin.testingclass.commonutil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class JdbcUtil {

	/*
	 * 日志收集
	 */
	private static final Logger log = LoggerFactory.getLogger(JdbcUtil.class);

	/*
	 * 创建JDBC连接
	 */
	public synchronized static Connection getConnection(String driver,String url, String userName, String password, String sid)
			throws SQLException {
		try {
			Class.forName(driver);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return DriverManager.getConnection(url, userName, password);
	}

	/*
	 * 关闭JDBC连接
	 */
	public static void closeConnection(final ResultSet rs, final Statement st,final Connection conn) {
		//关闭结果集
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				log.warn(e.toString(),e);
			}
		}
		//关闭语句
		if(st!=null){
			try {
				st.close();
			} catch (SQLException e) {
				log.warn(e.toString(),e);
			}
		}
		//关闭连接
		if(conn!=null){
			try {
				conn.close();
			} catch (SQLException e) {
				log.warn(e.toString(),e);
			}
		}
	}
}
